package com.kaixinff.name;

import org.json.JSONException;
import org.json.JSONObject;


public class User {
	public static class Gender {
		public static final Gender MALE = new Gender(0);
		public static final Gender FEMALE = new Gender(1);
		private int value;
		
		private Gender(int value) {
			this.value = value;
		}
		
		public int getValue() {
			return value;
		}
		
		public String toString() {
			if (value == 0) {
				return "male";
			} else {
				return "female";
			}
		}
	}
	
	private String id;
	
	private String name;
	
	private int gender;
	
	private int year;
	
	private int month;
	
	private int day;
	
	private int step;
	
	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getGender() {
		return gender;
	}

	public void setGender(int gender) {
		this.gender = gender;
	}

	public int getStep() {
		return step;
	}

	protected void setStep(int step) {
		this.step = step;
	}
	
	public boolean hasStep(int step) {
		return (this.step & step) != 0;
	}
	
	public void addStep(int step) {
		this.step = this.step | step;
	}
	
	public JSONObject toJSON() throws JSONException {
		JSONObject jobj = new JSONObject();
		jobj.put("id", this.getId());
		jobj.put("name", this.getName());
		jobj.put("gender", this.getGender());
		jobj.put("year", this.getYear());
		jobj.put("month", this.getMonth());
		jobj.put("day", this.getDay());
		jobj.put("step", this.getStep());
		return jobj;
	}
	
	public static User toUser(JSONObject jobj) throws JSONException {
		User user = new User();
		user.setId(jobj.getString("id"));
		user.setName(jobj.getString("name"));
		user.setGender(jobj.getInt("gender"));
		user.setYear(jobj.getInt("year"));
		user.setMonth(jobj.getInt("month"));
		user.setDay(jobj.getInt("day"));
		user.setStep(jobj.getInt("step"));
		return user;
	}
}
